package com.codeavatar.catools.base;

import android.content.Context;
import android.content.res.Configuration;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDexApplication;

import com.codeavatar.catools.ica.ICaConfig;

import org.jetbrains.annotations.NotNull;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public abstract class CaBaseApplication extends MultiDexApplication {
    //引用
    private static Context context;
    private static ICaConfig iconfig;
    private static ActivityLifecycleCallbacks lifecycleCallbacks;

    //++++++++++++++++++++++++++++++++++++++
    //++ 抽象方法
    //++++++++++++++++++++++++++++++++++++++
    public abstract void initServices();
    public abstract void initApp();
    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++
    public static Context getContext(){
        return context;
    }
    public static ICaConfig getIconfig(){
        return iconfig;
    }
    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++
    protected void setIconfig(@NotNull ICaConfig iconfig){
        CaBaseApplication.iconfig = iconfig;
    }
    protected void setLifecycleCallbacks(@NonNull ActivityLifecycleCallbacks lifecycleCallbacks){
        CaBaseApplication.lifecycleCallbacks = lifecycleCallbacks;
    }
    //++++++++++++++++++++++++++++++++++++++
    //++ Application 的生命周期
    //++++++++++++++++++++++++++++++++++++++

    // 程序启动时候执行
    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        if(null != lifecycleCallbacks){
            registerActivityLifecycleCallbacks(lifecycleCallbacks);
        }
        this.initApp();
        this.initServices();
    }

    //监听配置信息的改变事件（如屏幕旋转等）
    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    // 监听系统整体内存较低时候的事件
    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    // 监听系统内存不足处理事件
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
    }

    // 程序终止的时候执行
    @Override
    public void onTerminate() {
        if(null != lifecycleCallbacks){
            unregisterActivityLifecycleCallbacks(lifecycleCallbacks);
        }
        super.onTerminate();
    }
}
